#include "entimedthread.h"
#include <QDebug>

EnTimedThread::EnTimedThread( QObject* parent, int fps, LockedSemaphore* s, QApplication& a )
    : QThread( parent )
{
    waitForCleanUp = s;
    // convert fps to milliseconds and set the timer
    waitTime = ( 1/(float) fps ) * 1000;
    QObject::connect( &a, SIGNAL(aboutToQuit()), this, SLOT(handleQuit()) );
    moveToThread( this );
}

EnTimedThread::EnTimedThread( int fps, LockedSemaphore* s, QApplication& a )
    : QThread()
{
    waitForCleanUp = s;
    // convert fps to milliseconds and set the timer
    waitTime = ( 1/(float) fps ) * 1000;
    QObject::connect( &a, SIGNAL(aboutToQuit()), this, SLOT(handleQuit()) );
}


void EnTimedThread::update()
{
    //frames++;
    //qDebug() << "En Update: " << frames << endl;
}

void EnTimedThread::run()
{
    timer.start( waitTime );
    connect( &timer, SIGNAL( timeout() ), this, SLOT( update() ) );
    exec();
}

void EnTimedThread::handleQuit()
{
    timer.stop();
    this->exit();
    qDebug() << "Timed Thread closing..." << endl;
    waitForCleanUp->lockedRelease();
}

float EnTimedThread::getWaitTime()
{
    return waitTime;
}
